package com.snap.vseries.ui.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.TextView;

import com.snap.vseries.R;

import me.jessyan.autosize.utils.AutoSizeUtils;

public class ExportDialogView {
    TextView sureTv;
    TextView cancelTv;
    CheckBox[] buttons;
    private Context context;
    private OnSureListener listener;
    private View layout;
    private AlertDialog dialog;

    public ExportDialogView(Context context) {
        this.context = context;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        LayoutInflater inflater = LayoutInflater.from(context);
        layout = inflater.inflate(R.layout.export_dialog_view, null);
        sureTv = layout.findViewById(R.id.sure);
        cancelTv = layout.findViewById(R.id.cancel);
        buttons = new CheckBox[2];
        CheckBox button1 = layout.findViewById(R.id.export_pdf_tv);
        CheckBox button2 = layout.findViewById(R.id.export_xls_tv);
        buttons[0] = button1;
        buttons[1] = button2;
        dialog = builder.create();

    }

    public ExportDialogView setOnClickListener(OnSureListener listener) {
        this.listener = listener;
        return this;
    }

    public void show() {
        dialog.show();
        Window window = dialog.getWindow();
        if (window != null) {
            //自定义布局应该在这里添加，要在dialog.show()的后面
            window.setContentView(layout);
            //允许弹出系统键盘
            window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            window.setLayout(AutoSizeUtils.dp2px(context, 296), WindowManager.LayoutParams.WRAP_CONTENT);
            window.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bg_white_8));
        }
        sureTv.setOnClickListener(v -> {
            listener.onSureClick(buttons[0].isChecked(), buttons[1].isChecked());
        });
        cancelTv.setOnClickListener(v -> {
            dialog.dismiss();

        });

    }

    public void dismiss() {
        dialog.dismiss();
    }

    public interface OnSureListener {
        void onSureClick(boolean pdf, boolean xls);
    }
}
